/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.service;

import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.procurement.dao.MaterialsReceiveOrderDAO;
import com.javaeye.procurement.dao.SupplierOrderDAO;
import com.javaeye.procurement.dto.MaterialsReceiveOrder;
import com.javaeye.procurement.dto.MaterialsReceiveOrderDetail;
import com.javaeye.procurement.dto.SupplierOrder;
import com.javaeye.procurement.dto.SupplierOrderDetail;
import com.javaeye.procurement.web.SupplierCondition;
import com.javaeye.store.dao.MaterialsSKUDetailDAO;
import com.javaeye.store.dto.MaterialsSKUDetail;
import com.javaeye.common.util.DateUtils;
import com.javaeye.common.web.PageInfo;
import com.javaeye.delivery.dto.OrderCheck;
import com.javaeye.delivery.dao.CheckOrderDAO;
import com.javaeye.common.util.ListUtil;

public class MaterialsReceiveOrderService implements IMaterialsReceiveOrderService {
	
	private static Log log = LogFactory.getLog(MaterialsReceiveOrderService.class);
	
	private MaterialsReceiveOrderDAO dao;
	
	private MaterialsSKUDetailDAO materialsSkuDao;
	
	private SupplierOrderDAO supplierOrderDao;
	
	private CheckOrderDAO checkOrderDao;
	
	public void setDao(MaterialsReceiveOrderDAO dao) {
		this.dao = dao;
	}
	
	public void setCheckOrderDao(CheckOrderDAO checkOrderDao) {
		this.checkOrderDao = checkOrderDao;
	}
	
	public void setMaterialsSkuDao(MaterialsSKUDetailDAO materialsSkuDao) {
		this.materialsSkuDao = materialsSkuDao;
	}

	public void setSupplierOrderDao(SupplierOrderDAO supplierOrderDao) {
		this.supplierOrderDao = supplierOrderDao;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialsReceiveOrder> getMaterialsReceiveOrderList
		(SupplierCondition condition, PageInfo pageInfo) {
		List results = dao.getPageData(condition, pageInfo);
		if (results == null || results.size() == 0) {
			log.warn("在数据库中未找到收货单数据");
		}
		return results;
	}
	
	public MaterialsReceiveOrder getMaterialsReceiveOrderInfo(String orderId) {
		MaterialsReceiveOrder result = dao.getMaterialsReceiveOrder(orderId);
		if (result == null) {
			log.warn("在数据库中未找到收货单数据 id=" + orderId);
		}
		return result;
	}
	
	public List<OrderCheck> getCheckResult(String orderId) {
		List<OrderCheck> checkResults = checkOrderDao.getCheckResult(orderId);
		for (OrderCheck c : checkResults) {
			c.setResult(ListUtil.checkResultName(c.getResult()));
		}
		return checkResults;
	}

	@Override
	public void saveMaterialsReceiveOrder(MaterialsReceiveOrder order) {
		if (null == order.getId() || "".equals(order.getId())) {
			String orderId = order.getSupplierOrderId() + "-";
			int maxId = dao.getMaxOrderId(orderId);
			String newOrderId = orderId + String.format("%02d", maxId + 1);
			log.debug("生成的收货单号：" + newOrderId);
			order.setId(newOrderId);
		}
		order.setStatus(MaterialsReceiveOrder.ORDER_STATES_CREATED);
		order.setUpdateDate(DateUtils.now());
		dao.saveMaterialsReceiveOrder(order);
	}
	
	/**
	 * 删除收货单
	 * @param orderId
	 */
	public void removeReceiveOrder(String orderId) {
		dao.removeReceiveOrder(orderId);
	}
	
	public void saveMaterialsReceiveInfo(MaterialsReceiveOrder order) {
		order.setStatus(MaterialsReceiveOrder.ORDER_STATES_RECEIVED);
		order.setUpdateDate(DateUtils.now());
		dao.saveMaterialsReceiveOrder(order);
		
		Set<MaterialsReceiveOrderDetail> details = order.getOrderDetails();
		for (MaterialsReceiveOrderDetail detail : details) {
			//更新待入库库存
			int materialsId = detail.getMaterialsId();
			
			MaterialsSKUDetail skuDetail = materialsSkuDao.getMaterialsSKUDetail(materialsId);
			skuDetail.setWaitNumber(skuDetail.getWaitNumber() + detail.getRealNumber());
			materialsSkuDao.saveMaterialsSKUDetail(skuDetail);
		}
		
		//更新已收货数量
		SupplierOrder supplierOrder = supplierOrderDao.getSupplierOrder(order.getSupplierOrderId());
		Set<SupplierOrderDetail> supplierOrderDetails = supplierOrder.getOrderDetails();
		for(SupplierOrderDetail sDetail : supplierOrderDetails) {
			int materialsId = sDetail.getMaterialsId(); 
			float receiveNumber = getReceiveNumber(materialsId, details);
			sDetail.setReceiveNumber(sDetail.getReceiveNumber() + receiveNumber);
		}
		
		
		//判断是否全部收货完成
		boolean allReceive = checkAllReceive(supplierOrderDetails);
		if (allReceive) {
			supplierOrder.setStatus(SupplierOrder.ORDER_STATES_ALL_RECEIVE);
		}
		supplierOrderDao.saveSupplierOrder(supplierOrder);
	}
	
	private boolean checkAllReceive(Set<SupplierOrderDetail> supplierOrderDetails) {
		for (SupplierOrderDetail sDetail : supplierOrderDetails) {
			float orderNumber = sDetail.getNumber(); 
			float receiveNumber = sDetail.getReceiveNumber();
			if (receiveNumber < orderNumber) {
				return false;
			}
		}
		return true;
	}
	
	private float getReceiveNumber(int materialsId, Set<MaterialsReceiveOrderDetail> details) {
		for (MaterialsReceiveOrderDetail detail : details) {
			int id = detail.getMaterialsId();
			if (materialsId == id) {
				return detail.getRealNumber();
			}
		}
		return 0;
	}
	
	/**
	 * 根据采购订单返回对应的收货单数
	 * @param supplierOrderId
	 * @return
	 */
	public long getMaterialsReceiveOrderCount(String supplierOrderId) {
		long count = dao.getMaterialsReceiveOrderCount(supplierOrderId);
		//log.warn("采购订单:" + supplierOrderId + ",收货单数:" + count);
		return count;
	}
	
}
